<?php
namespace Home\Controller;
use Common\Controller\CommonController;
class AptController extends CommonController {
	function index(){
		$where['name']=I('key');		
		if($where['name']!=""){
		$count=M('apt')->where($where)->count();
		$page=new \Think\Page($count,10);
		$show=$page->show();	
		$re=M('apt')->limit($page->firstRow.','.$page->listRows)->where($where)->select();		
		foreach ($re as $key => $value) {			
			$cid=$value['cid'];
			$sid=$value['sid'];
			$aid=$value['aid'];
			$area=M('area')->where('id='.$aid)->find();
			$city=M('city')->where('id='.$cid)->find();
			$sc=M('sc')->where('id='.$sid)->find();
			$value['city']=$city['city'];
			$value['area']=$area['area'];
			$value['sc']=$sc['sc'];
			$a[]=$value;
		}	
		$re=$a;		
		}else{	
		$count=M('apt')->count();
		$page=new \Think\Page($count,10);
		$show=$page->show();	
		$re=M('apt')->limit($page->firstRow.','.$page->listRows)->select();		
		foreach ($re as $key => $value) {
			$sid=$value['sid'];
			$cid=$value['cid'];
			$aid=$value['aid'];
			$area=M('area')->where('id='.$aid)->find();
			$city=M('city')->where('id='.$cid)->find();
			$sc=M('sc')->where('id='.$sid)->find();
			$value['city']=$city['city'];
			$value['area']=$area['area'];
			$value['sc']=$sc['sc'];
			$a[]=$value;
		}	
		$re=$a;
		}
		$this->assign('page',$show);
		$this->assign('list',$re);
		$this->display('index');
	}	
	function addlist(){
			$re=M('city')->select();
			$rs=M('sc')->select();		
			$res=M('fw')->select();	
			$this->assign('list',$re);
			$this->assign('sc',$rs);
			$this->assign('arr',$res);
			$this->display('add');		
	}
	function fangxing(){
			$id=I('id');
			$this->assign('id',$id);
			$this->display('fangxing');		
	}	
	function fangxinglist(){
			$id=I('id');
			$re=M('fangxing')->where('hid='.$id)->select();	
			
			$preg ="/<img src=\"(.+?)\".*?>/";//只能有src	
			foreach ($re as $key => $value) {
			preg_match_all($preg,html_entity_decode($value['img']),$listimg);		
			$listimg_arr=array_unique($listimg[1]);//去除数组中重复的值			
			$value['src']=$listimg_arr[0];
			$a[]=$value;
			}
			$re=$a;
		
			$this->assign('id',$id);
			$this->assign('list',$re);			
			$this->display('fangxinglist');		
	}	
	function addfangxing(){
			$hid=I('hid');
			$img=I('img');
			$tuijian=I('tuijian');
			$jiage=I('jiage');
			$huxing=I('huxing');
			$mianji=I('mianji');
			$pingjia=I('pingjia');
			$data=array(
					'img'=>html_entity_decode($img),
					'tuijian'=>$tuijian,
					'hid'=>$hid,
					'jiage'=>$jiage,
					'huxing'=>$huxing,
					'mianji'=>$mianji,
					'pingjia'=>$pingjia,					
					'ctime'=>time(),
					'utime'=>time(),
			);			
			$res=M('fangxing')->data($data)->add();		
			if($res){
			$this->redirect('Apt/fangxinglist',array('id'=>$hid));		
			}		
	}
	function fangxingup(){
			$id=I('id');
			$re=M('fangxing')->where('id='.$id)->find();
			
			$this->assign('id',$id);
			$this->assign('list',$re);			
			$this->display('upfangxing');		
		
	}	
	function upfangxing(){
			$id=I('id');		
			$img=I('img');
			$tuijian=I('tuijian');
			$jiage=I('jiage');
			$huxing=I('huxing');
			$mianji=I('mianji');
			$pingjia=I('pingjia');
			$data=array(
					'img'=>html_entity_decode($img),
					'tuijian'=>$tuijian,
					'jiage'=>$jiage,
					'huxing'=>$huxing,
					'mianji'=>$mianji,
					'pingjia'=>$pingjia,					
					'utime'=>time(),
			);						
			$res=M('fangxing')->where('id='.$id)->data($data)->save();		
			$re=M('fangxing')->where('id='.$id)->find();			
			if($res){
			$this->redirect('Apt/fangxinglist',array('id'=>$re['hid']));		
			}					
	}

	function delfangxing(){
			$id=I('id');
			$re=M('fangxing')->where('id='.$id)->find();
			$preg ="/<img src=\"(.+?)\".*?>/";//只能有src		
			preg_match_all($preg,html_entity_decode($re['img']),$listimg);		
			$listimg_arr=array_unique($listimg[1]);//去除数组中重复的值	
			$ping=array('/'=>'./');//路径
			//$ping=array('/uchoice'=>'.');//路径 本地
			foreach($listimg_arr as $key){				
					$listimg_n=strtr($key,$ping);//替换路径						
					unlink($listimg_n);
		    }	
		
			$res=M('fangxing')->where('id='.$id)->delete();	
		
			if($res){
			$this->redirect('Apt/fangxinglist',array('id'=>$re['hid']));		
			}			
	}
	
		function listimg(){
			$re=M('listimg')->select();
			$count=M('listimg')->count();
			$preg ="/<img src=\"(.+?)\".*?>/";//只能有src		
			foreach ($re as $key => $value) {
			preg_match_all($preg,html_entity_decode($value['img']),$listimg);		
			$listimg_arr=array_unique($listimg[1]);//去除数组中重复的值			
			$value['src']=$listimg_arr[0];
			$a[]=$value;
			}
			$re=$a;
			
			$this->assign('list',$re);
			$this->assign('count',$count);
			$this->display('list');		
	}
	
	
	function listbn(){			
			$this->display();		
	}
	
	function listimgadd(){
			$img=I('img');
			$ms=I('ms');
			$url=I('url');
			$data=array(
			'img'=>html_entity_decode($img),
			'ms'=>$ms,
			'url'=>$url
			);			
			$res=M('listimg')->data($data)->add();		
			if($res){
			$this->redirect('Apt/listimg');		
			}			
		
	}
	function listimgdel(){
			$id=I('id');	
			$re=M('listimg')->where("id=".$id)->find();
			$preg ="/<img src=\"(.+?)\".*?>/";//只能有src		
			preg_match_all($preg,html_entity_decode($re['img']),$listimg);		
			$listimg_arr=$listimg[1][0];//去除数组中重复的值	
			
			$ping=array('/'=>'./');//路径
			//$ping=array('/uchoice'=>'.');//路径 本地
			$listimg_n=strtr($listimg_arr,$ping);//替换路径		
					
			unlink($listimg_n);
		   
			$res=M('listimg')->where("id=".$id)->delete();		
			if($res){
			$this->redirect('Apt/listimg');		
			}			
		
	}
	
	
	function addapt(){
			$fw=I('fw');			
			$name=I('name');
			$city=I('city');
			$area=I('area');
			$sc=I('sc');
			$tel=I('tel');
			$email=I('email');
			$dz=I('dz');
			$url=I('url');
			$content=I('content');
			$jtxl=I('jtxl');			
			$listimg=I('listimg');		
			$kvimg=I('kvimg');
			$motimg=I('motimg');
			$psimg=I('psimg');
			$map=I('map');
			
			$data=array(
					'name'=>$name,
					'cid'=>$city,
					'aid'=>$area,
					'sid'=>$sc,
					'tel'=>$tel,
					'email'=>$email,
					'dz'=>$dz,
					'url'=>$url,
					'fw'=>implode(",",$fw),
					'content'=>$content,
					'jtxl'=>$jtxl,
					'listimg'=>html_entity_decode($listimg),
					'kvimg'=>html_entity_decode($kvimg),					
					'motimg'=>html_entity_decode($motimg),
					'psimg'=>html_entity_decode($psimg),
					'map'=>$map,
					'ctime'=>time(),
					'utime'=>time(),
			);			
		$res=M('apt')->data($data)->add();			
		if($res){
		$this->redirect('Apt/index');	
		}	
	}
	
	
	
	function uplist(){
			$where['id']=I('id');
			$res=M('apt')->where($where)->find();	
			$re=M('city')->select();
			$area=M('area')->where('cid='.$res['cid'])->select();
			
			$rs=M('sc')->where('aid='.$res['aid'])->select();
		 
			$rey=M('fw')->select();	
				
			$fw=explode(",", $res['fw']);
			
			$this->assign('fw',$fw);
			$this->assign('area',$area);
			$this->assign('sc',$rs);
			$this->assign('list',$re);
			$this->assign('arr',$res);
			$this->assign('ar',$rey);
			$this->display('up');		
	}	

	function upapt(){
			$fw=I('fw');	
			$id=I('id');
			$name=I('name');
			$city=I('city');
			$area=I('area');
			$sc=I('sc');
			$tel=I('tel');
			$email=I('email');
			$dz=I('dz');
			$url=I('url');
			$content=I('content');
			$jtxl=I('jtxl');			
			$listimg=I('listimg');		
			$kvimg=I('kvimg');
			$motimg=I('motimg');
			$psimg=I('psimg');
			$map=I('map');
			
			$data=array(
					'name'=>$name,
					'cid'=>$city,
					'aid'=>$area,
					'sid'=>$sc,
					'tel'=>$tel,
					'email'=>$email,
					'dz'=>$dz,
					'url'=>$url,
					'fw'=>implode(",",$fw),
					'content'=>$content,
					'jtxl'=>$jtxl,
					'listimg'=>html_entity_decode($listimg),
					'kvimg'=>html_entity_decode($kvimg),					
					'motimg'=>html_entity_decode($motimg),
					'psimg'=>html_entity_decode($psimg),
					'map'=>$map,				
					'utime'=>time(),
			);			
			
		$res=M('apt')->data($data)->where('id='.$id)->save();		
		if($res){
		$this->redirect('Apt/index');	
		}		
	
	}
	
	function delapt(){
		$id=I('id');
		$where['id']=$id;
		$re=M('apt')->where($where)->find();    
		$res=M('fangxing')->where('hid='.$id)->select();
		$preg ="/<img src=\"(.+?)\".*?>/";//只能有src
		//$ping=array('/uchoice'=>'.');//路径 本地
		$ping=array('/'=>'./');//路径
		
	
		foreach ($res as $k => $v) {
			$img=$v['img'];		
			preg_match_all($preg,html_entity_decode($img),$limg);		
			$img_arr=array_unique($limg[1]);//去除数组中重复的值			
			foreach($img_arr as $key){	
				$img_n=strtr($key,$ping);//替换路径						
				unlink($img_n);
		 	 }		
		}			
		preg_match_all($preg,html_entity_decode($re['listimg']),$listimg);		
		$listimg_arr=array_unique($listimg[1]);//去除数组中重复的值			
		foreach($listimg_arr as $key){				
				$listimg_n=strtr($key,$ping);//替换路径			
				unlink($listimg_n);
		    }					
		preg_match_all($preg,html_entity_decode($re['kvimg']),$kvimg);
		$kvimg_arr=array_unique($kvimg[1]);	
		foreach($kvimg_arr as $key){				
				$kvimg_n=strtr($key,$ping);			
				unlink($kvimg_n);
		    }
		preg_match_all($preg,html_entity_decode($re['motimg']),$motimg);
		$motimg_arr=array_unique($motimg[1]);	
		foreach($motimg_arr as $key){				
				$motimg_n=strtr($key,$ping);			
				unlink($motimg_n);
		    }
		preg_match_all($preg,html_entity_decode($re['psimg']),$psimg);
		$psimg_arr=array_unique($psimg[1]);	
		foreach($psimg_arr as $key){				
				$psimg_n=strtr($key,$ping);			
				unlink($psimg_n);
		    }		
		$re=M('apt')->where($where)->delete();
		$res=M('fangxing')->where('hid='.$id)->delete();
		if($re){
		$this->redirect('Apt/index');
		}
	}
	

	function areald(){
		$cid=I('cid');
		$where['cid']=$cid;
		$re=M('area')->where($where)->select();
		if($re){
		$this->ajaxReturn(array('s'=>"true",'m'=>$re));	
		}else{
		$this->ajaxReturn(array('s'=>"false"));	
		}
		
	}
	
		function scld(){
		$aid=I('aid');
		$where['aid']=$aid;
		$re=M('sc')->where($where)->select();
		if($re){
		$this->ajaxReturn(array('s'=>"true",'m'=>$re));	
		}else{
		$this->ajaxReturn(array('s'=>"false"));	
		}
		
	}
	
	
	
	
	
	
	
}